<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.19"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Modbus cpp: include/MB/modbusResponse.hpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">Modbus cpp
   &#160;<span id="projectnumber">0.1</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.19 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d44c64559bbebec7f509842c48db8b23.html">include</a></li><li class="navelem"><a class="el" href="dir_f8aebf652183de33714d471cbd098255.html">MB</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">modbusResponse.hpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">// Modbus for c++ &lt;https://github.com/Mazurel/Modbus&gt;</span></div>
<div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment">// Copyright (c) 2020 Mateusz Mazur aka Mazurel</span></div>
<div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment">// Licensed under: MIT License &lt;http://opensource.org/licenses/MIT&gt;</span></div>
<div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160; </div>
<div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="preprocessor">#pragma once</span></div>
<div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160; </div>
<div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="preprocessor">#include &lt;algorithm&gt;</span></div>
<div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="preprocessor">#include &lt;cstdint&gt;</span></div>
<div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="preprocessor">#include &lt;stdexcept&gt;</span></div>
<div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="preprocessor">#include &lt;string&gt;</span></div>
<div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="preprocessor">#include &lt;vector&gt;</span></div>
<div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160; </div>
<div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="preprocessor">#include &quot;modbusCell.hpp&quot;</span></div>
<div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="preprocessor">#include &quot;modbusException.hpp&quot;</span></div>
<div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="preprocessor">#include &quot;modbusRequest.hpp&quot;</span></div>
<div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="preprocessor">#include &quot;modbusUtils.hpp&quot;</span></div>
<div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160; </div>
<div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespaceMB.html">MB</a> {</div>
<div class="line"><a name="l00026"></a><span class="lineno"><a class="line" href="classMB_1_1ModbusResponse.html">   26</a></span>&#160;<span class="keyword">class </span><a class="code" href="classMB_1_1ModbusResponse.html">ModbusResponse</a> {</div>
<div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;<span class="keyword">private</span>:</div>
<div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;  uint8_t _slaveID;</div>
<div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;  <a class="code" href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">utils::MBFunctionCode</a> _functionCode;</div>
<div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160; </div>
<div class="line"><a name="l00031"></a><span class="lineno">   31</span>&#160;  uint16_t _address;</div>
<div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;  uint16_t _registersNumber;</div>
<div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160; </div>
<div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;  std::vector&lt;ModbusCell&gt; _values;</div>
<div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160; </div>
<div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;<span class="keyword">public</span>:</div>
<div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;  <span class="keyword">explicit</span> <a class="code" href="classMB_1_1ModbusResponse.html#abb81c341259007fffa42f59d5a3bdf6d">ModbusResponse</a>(std::vector&lt;uint8_t&gt; inputData, <span class="keywordtype">bool</span> CRC = <span class="keyword">false</span>);</div>
<div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160; </div>
<div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;  <span class="comment">/*</span></div>
<div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;<span class="comment">   * @description Constructs Response from raw data</span></div>
<div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;<span class="comment">   * @params inputData is a vector of bytes that will be interpreted</span></div>
<div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;<span class="comment">   * @throws ModbusException</span></div>
<div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;<span class="comment">   **/</span></div>
<div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;  <span class="keyword">static</span> <a class="code" href="classMB_1_1ModbusResponse.html">ModbusResponse</a> fromRaw(std::vector&lt;uint8_t&gt; inputData) {</div>
<div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;    <span class="keywordflow">return</span> <a class="code" href="classMB_1_1ModbusResponse.html#abb81c341259007fffa42f59d5a3bdf6d">ModbusResponse</a>(inputData);</div>
<div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;  }</div>
<div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;  <span class="comment">/*</span></div>
<div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;<span class="comment">   * @description Constructs Request from raw data and checks it&#39;s CRC</span></div>
<div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;<span class="comment">   * @params inputData is a vector of bytes that will be interpreted</span></div>
<div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;<span class="comment">   * @throws ModbusException</span></div>
<div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;<span class="comment">   * @note This methods performs CRC check that may throw ModbusException on</span></div>
<div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;<span class="comment">   * invalid CRC</span></div>
<div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;<span class="comment">   **/</span></div>
<div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;  <span class="keyword">static</span> <a class="code" href="classMB_1_1ModbusResponse.html">ModbusResponse</a> fromRawCRC(std::vector&lt;uint8_t&gt; inputData) {</div>
<div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;    <span class="keywordflow">return</span> <a class="code" href="classMB_1_1ModbusResponse.html#abb81c341259007fffa42f59d5a3bdf6d">ModbusResponse</a>(inputData, <span class="keyword">true</span>);</div>
<div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;  }</div>
<div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160; </div>
<div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;  <a class="code" href="classMB_1_1ModbusResponse.html#abb81c341259007fffa42f59d5a3bdf6d">ModbusResponse</a>(uint8_t slaveId = 0,</div>
<div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;                 <a class="code" href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">utils::MBFunctionCode</a> functionCode =</div>
<div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;                     <span class="keyword">static_cast&lt;</span><a class="code" href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">utils::MBFunctionCode</a><span class="keyword">&gt;</span>(0),</div>
<div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;                 uint16_t address = 0, uint16_t registersNumber = 0,</div>
<div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;                 std::vector&lt;ModbusCell&gt; values = {});</div>
<div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160; </div>
<div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;  <a class="code" href="classMB_1_1ModbusResponse.html#abb81c341259007fffa42f59d5a3bdf6d">ModbusResponse</a>(<span class="keyword">const</span> <a class="code" href="classMB_1_1ModbusResponse.html">ModbusResponse</a> &amp;) = <span class="keywordflow">default</span>;</div>
<div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160; </div>
<div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;  [[nodiscard]] std::string <a class="code" href="classMB_1_1ModbusResponse.html#ac5ff14306fc2b4886241602e6cdfd12e">toString</a>() <span class="keyword">const</span>;</div>
<div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;  [[nodiscard]] std::vector&lt;uint8_t&gt; toRaw() <span class="keyword">const</span>;</div>
<div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;  <span class="keywordtype">void</span> <a class="code" href="classMB_1_1ModbusResponse.html#a637d66a0504981767fc185f64f57059c">from</a>(<span class="keyword">const</span> <a class="code" href="classMB_1_1ModbusRequest.html">ModbusRequest</a> &amp;);</div>
<div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160; </div>
<div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;  [[nodiscard]] <a class="code" href="namespaceMB_1_1utils.html#a0b3590994f5ba26cd61929f5a394ab70">utils::MBFunctionType</a> functionType()<span class="keyword"> const </span>{</div>
<div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;    <span class="keywordflow">return</span> <a class="code" href="namespaceMB_1_1utils.html#ab24a81a029cf6c4765eab5d74b7d8dff">utils::functionType</a>(_functionCode);</div>
<div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;  }</div>
<div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160; </div>
<div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;  [[nodiscard]] <a class="code" href="namespaceMB_1_1utils.html#a8098c5bcb6c33b219bafdee42334e260">utils::MBFunctionRegisters</a> functionRegisters()<span class="keyword"> const </span>{</div>
<div class="line"><a name="l00094"></a><span class="lineno">   94</span>&#160;    <span class="keywordflow">return</span> <a class="code" href="namespaceMB_1_1utils.html#ad967d0ef18fecbc1a231143db3793991">utils::functionRegister</a>(_functionCode);</div>
<div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;  }</div>
<div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160; </div>
<div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;  [[nodiscard]] uint8_t slaveID()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> _slaveID; }</div>
<div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;  [[nodiscard]] <a class="code" href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">utils::MBFunctionCode</a> functionCode()<span class="keyword"> const </span>{</div>
<div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;    <span class="keywordflow">return</span> _functionCode;</div>
<div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;  }</div>
<div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;  [[nodiscard]] uint16_t registerAddress()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> _address; }</div>
<div class="line"><a name="l00102"></a><span class="lineno">  102</span>&#160;  [[nodiscard]] uint16_t numberOfRegisters()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> _registersNumber; }</div>
<div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;  [[nodiscard]] <span class="keyword">const</span> std::vector&lt;ModbusCell&gt; &amp;registerValues()<span class="keyword"> const </span>{</div>
<div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;    <span class="keywordflow">return</span> _values;</div>
<div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;  }</div>
<div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160; </div>
<div class="line"><a name="l00107"></a><span class="lineno">  107</span>&#160;  <span class="keywordtype">void</span> setSlaveId(uint8_t slaveId) { _slaveID = slaveId; }</div>
<div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;  <span class="keywordtype">void</span> setFunctionCode(<a class="code" href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">utils::MBFunctionCode</a> functionCode) {</div>
<div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;    _functionCode = functionCode;</div>
<div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;  }</div>
<div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;  <span class="keywordtype">void</span> setAddress(uint16_t address) { _address = address; }</div>
<div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;  <span class="keywordtype">void</span> setRegistersNumber(uint16_t registersNumber) {</div>
<div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;    _registersNumber = registersNumber;</div>
<div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;    _values.resize(registersNumber);</div>
<div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;  }</div>
<div class="line"><a name="l00116"></a><span class="lineno">  116</span>&#160;  <span class="keywordtype">void</span> setValues(<span class="keyword">const</span> std::vector&lt;ModbusCell&gt; &amp;values) { _values = values; }</div>
<div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;};</div>
<div class="line"><a name="l00118"></a><span class="lineno">  118</span>&#160;} <span class="comment">// namespace MB</span></div>
</div><!-- fragment --></div><!-- contents -->
<div class="ttc" id="aclassMB_1_1ModbusResponse_html_ac5ff14306fc2b4886241602e6cdfd12e"><div class="ttname"><a href="classMB_1_1ModbusResponse.html#ac5ff14306fc2b4886241602e6cdfd12e">MB::ModbusResponse::toString</a></div><div class="ttdeci">std::string toString() const</div><div class="ttdoc">Converts object to it's string representation.</div><div class="ttdef"><b>Definition:</b> modbusResponse.cpp:108</div></div>
<div class="ttc" id="anamespaceMB_1_1utils_html_a8098c5bcb6c33b219bafdee42334e260"><div class="ttname"><a href="namespaceMB_1_1utils.html#a8098c5bcb6c33b219bafdee42334e260">MB::utils::MBFunctionRegisters</a></div><div class="ttdeci">MBFunctionRegisters</div><div class="ttdoc">Simplified register types.</div><div class="ttdef"><b>Definition:</b> modbusUtils.hpp:156</div></div>
<div class="ttc" id="aclassMB_1_1ModbusRequest_html"><div class="ttname"><a href="classMB_1_1ModbusRequest.html">MB::ModbusRequest</a></div><div class="ttdef"><b>Definition:</b> modbusRequest.hpp:25</div></div>
<div class="ttc" id="anamespaceMB_1_1utils_html_aa8ebb89b73bf4d0e3485b6341a50f116"><div class="ttname"><a href="namespaceMB_1_1utils.html#aa8ebb89b73bf4d0e3485b6341a50f116">MB::utils::MBFunctionCode</a></div><div class="ttdeci">MBFunctionCode</div><div class="ttdoc">All modbus standard function codes + Undefined one.</div><div class="ttdef"><b>Definition:</b> modbusUtils.hpp:114</div></div>
<div class="ttc" id="anamespaceMB_1_1utils_html_a0b3590994f5ba26cd61929f5a394ab70"><div class="ttname"><a href="namespaceMB_1_1utils.html#a0b3590994f5ba26cd61929f5a394ab70">MB::utils::MBFunctionType</a></div><div class="ttdeci">MBFunctionType</div><div class="ttdoc">Simplified function types.</div><div class="ttdef"><b>Definition:</b> modbusUtils.hpp:134</div></div>
<div class="ttc" id="anamespaceMB_1_1utils_html_ab24a81a029cf6c4765eab5d74b7d8dff"><div class="ttname"><a href="namespaceMB_1_1utils.html#ab24a81a029cf6c4765eab5d74b7d8dff">MB::utils::functionType</a></div><div class="ttdeci">MBFunctionType functionType(const MBFunctionCode code)</div><div class="ttdoc">Checks &quot;Function type&quot;, according to MBFunctionType.</div><div class="ttdef"><b>Definition:</b> modbusUtils.hpp:137</div></div>
<div class="ttc" id="aclassMB_1_1ModbusResponse_html"><div class="ttname"><a href="classMB_1_1ModbusResponse.html">MB::ModbusResponse</a></div><div class="ttdef"><b>Definition:</b> modbusResponse.hpp:26</div></div>
<div class="ttc" id="aclassMB_1_1ModbusResponse_html_a637d66a0504981767fc185f64f57059c"><div class="ttname"><a href="classMB_1_1ModbusResponse.html#a637d66a0504981767fc185f64f57059c">MB::ModbusResponse::from</a></div><div class="ttdeci">void from(const ModbusRequest &amp;)</div><div class="ttdoc">Fills all data from associated request.</div><div class="ttdef"><b>Definition:</b> modbusResponse.cpp:188</div></div>
<div class="ttc" id="aclassMB_1_1ModbusResponse_html_abb81c341259007fffa42f59d5a3bdf6d"><div class="ttname"><a href="classMB_1_1ModbusResponse.html#abb81c341259007fffa42f59d5a3bdf6d">MB::ModbusResponse::ModbusResponse</a></div><div class="ttdeci">ModbusResponse(std::vector&lt; uint8_t &gt; inputData, bool CRC=false)</div><div class="ttdoc">Constructs Response from raw data.</div><div class="ttdef"><b>Definition:</b> modbusResponse.cpp:30</div></div>
<div class="ttc" id="anamespaceMB_html"><div class="ttname"><a href="namespaceMB.html">MB</a></div><div class="ttdef"><b>Definition:</b> modbusCell.hpp:14</div></div>
<div class="ttc" id="anamespaceMB_1_1utils_html_ad967d0ef18fecbc1a231143db3793991"><div class="ttname"><a href="namespaceMB_1_1utils.html#ad967d0ef18fecbc1a231143db3793991">MB::utils::functionRegister</a></div><div class="ttdeci">MBFunctionRegisters functionRegister(const MBFunctionCode code)</div><div class="ttdoc">Get register type based on function code.</div><div class="ttdef"><b>Definition:</b> modbusUtils.hpp:164</div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="http://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.8.19
</small></address>
</body>
</html>
